package questionmanager;

import gui.CustomJTextArea;
import gui.GUIMain;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import rmi.Answer;
import rmi.Question;

public class GUIQuestionCell extends JPanel{

	private static final long serialVersionUID = 1L;
	public static final int MAX_PRIORITY = 1;
	public static final int MIN_PRIORITY = 0;
	
	private Question question;
	private JTextArea questionString;
	private JLabel image;
	private JSlider priority = null;
	private boolean isSelected;
	JPanel priPanel;
	
	public GUIQuestionCell( boolean isShowPriority) {
		setLayout(new GridBagLayout());
		setOpaque(false);
		setBorder(new EmptyBorder(2, 10, 0, 10));
		setMinimumSize(new Dimension(100, 100));
		question = new Question();
		questionString = new JTextArea();
		questionString.setEditable(false);
		questionString.setOpaque(false);
		questionString.setWrapStyleWord(true);
		questionString.setLineWrap(true);
		questionString.setFont(getFont().deriveFont(16f));
		
		JPanel panel = new JPanel(new BorderLayout());
		panel.setOpaque(false);
		panel.add(questionString, BorderLayout.NORTH);
		
		image = new JLabel();
		
		if(isShowPriority){
			priority = new JSlider(JSlider.HORIZONTAL, MIN_PRIORITY, MAX_PRIORITY,MAX_PRIORITY);
			priority.setMajorTickSpacing(MAX_PRIORITY);
			priority.setMinorTickSpacing(1);
			priority.setPaintTicks(true);
			priority.setOpaque(false);
			priority.addChangeListener(new ChangeListener() {
				@Override
				public void stateChanged(ChangeEvent arg0) {	
					if(!priority.getValueIsAdjusting()){
						int temp = priority.getValue();
						GUIMain.getDb().UpdateQuestion(question.getQuestionID(),
								question.getQuestion(),question.getQuestionImageLink(), question.getQuestionType(),temp);
					}
				}
			});
			priPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			priority.setPreferredSize(new Dimension(100,20));
			priPanel.add(new JLabel("Hiển thị: "));
			priPanel.add(priority);
			priPanel.setOpaque(false);
		}
	}

	@Override
	protected void paintComponent(Graphics g) {
		g.setColor(new Color(255,255,255,0));
		if(isSelected){
			g.setColor(new Color(0, 0, 0,10));
		}
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		super.paintComponent(g);
	}
	
	public void setQuestion(Question param){
		this.question = param;
		updateGUI();
	}
	
	private void updateGUI(){
		removeAll();
		questionString.setText(this.question.getQuestion());
		GridBagConstraints c = new GridBagConstraints();
		int count = 0;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1;
		c.gridx = 0;
		c.gridy = count;
		count++;
		add(questionString,c);
		c.gridy = count;
		count++;
		add(image,c);
		try {
			ImageIcon imIcon = new ImageIcon(GUIMain.getDb().getQuestionsByQuestionID(question.getQuestionID()).getQuestionImageLink());
			
			Image im = imIcon.getImage();
			int width = im.getWidth(null);
			int height = im.getHeight(null);
			
			if(width > 550){
				width = 550;
			}
			
			if(height > 200){
				height = 200;
			}
				
			im = im.getScaledInstance(width, height, Image.SCALE_DEFAULT);
			image.setIcon(new ImageIcon(im));
			

		} catch (SQLException e2) {
			e2.printStackTrace();
		}

		LinkedHashMap<Integer, Answer> temp;
		try {
			temp = GUIMain.getDb().getQuestionsByQuestionID(question.getQuestionID()).getMapAnswers();
		} catch (SQLException e2) {
			e2.printStackTrace();
			return;
		}
		final int trueAnswer;
		try {
			trueAnswer = GUIMain.getDb().GetTrueAnswerByQuestion(question.getQuestionID());
		} catch (SQLException e1) {
			return;
		}
		
		for(final Entry<Integer, Answer> e : temp.entrySet()){
			final Answer answer = e.getValue();
			JCheckBox cb = new JCheckBox();
			JTextArea ta = new CustomJTextArea();
			ta.setText(answer.getAnswerContent());
			cb.setSelected(answer.isShow());
			cb.addItemListener(new ItemListener() {
				@Override
				public void itemStateChanged(ItemEvent arg0) {
					boolean isShowInput = ((JCheckBox)arg0.getSource()).isSelected();
					boolean isTrue = false;
					if(trueAnswer==e.getKey().intValue()){
						isTrue = true;
					}
					if(isTrue&&!isShowInput){
						JOptionPane.showMessageDialog(null,
								"Không thể ẩn kết quả đúng", "Thông báo", JOptionPane.WARNING_MESSAGE);
						((JCheckBox)arg0.getSource()).setSelected(true);
						return;
					}
					
					GUIMain.getDb().UpdateAnswer(e.getKey().intValue(), answer.getAnswerContent(),
							question.getQuestionID(), isTrue, isShowInput);
				}
			});
			cb.setOpaque(false);
			c.gridy = count;
			count++;
			
			JPanel answerCell = new JPanel(new GridBagLayout());
			answerCell.setOpaque(false);
			GridBagConstraints gnc = new GridBagConstraints();
			gnc.fill = GridBagConstraints.HORIZONTAL;
			gnc.gridx = 0;
			gnc.gridy = 0;
			answerCell.add(cb,gnc);
			gnc.gridx = 1;
			gnc.gridy = 0;
			gnc.weightx = 1;
			answerCell.add(ta,gnc);
			add(answerCell,c);
			
		}
		if(priority!=null){
			int pri;
			try {
				pri = GUIMain.getDb().getQuestionsByQuestionID(question.getQuestionID()).getPriority();
			} catch (SQLException e1) {
				e1.printStackTrace();
				return;
			}
			if(pri>1) pri = 1;
			if(pri<0) pri = 0;
			priority.setValue(pri);
			c.gridy = count;
			count++;
			add(priPanel,c);
		}
	}
	
	public Question getQuestion(){
		return this.question;
	}

	public boolean isSelected() {
		return isSelected;
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
	}
}
